# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >

_realname=scipy
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.1.0
pkgrel=1
pkgdesc="SciPy is open-source software for mathematics, science, and engineering. (mingw-w64)"
arch=('any')
url="https://www.scipy.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-openblas"
             "${MINGW_PACKAGE_PREFIX}-python2-numpy"
             "${MINGW_PACKAGE_PREFIX}-python3-numpy"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/scipy/scipy/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('878352408424dffaa695ffedf2f9f92844e116686923ed9aa8626fc30d32cfd1')

prepare() {
  cp -a ${_realname}-${pkgver} ${_realname}-py2-${pkgver}
  cp -a ${_realname}-${pkgver} ${_realname}-py3-${pkgver}

  cd ${_realname}-py2-${pkgver}
  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
    -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
    -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
    -i $(find . -name '*.py')
}

build() {
  LDFLAGS="${LDFLAGS} -Wall -shared"

  # build for python2
  cd ${srcdir}/${_realname}-py2-${pkgver}
  local result="fail"
  while [ "$result" = "fail" ]; do
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 build && result="success"
  done

  # build for python3
  cd ${srcdir}/${_realname}-py3-${pkgver}
  result="fail"
  while [ "$result" = "fail" ]; do
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 build && result="success"
  done
}

package_python2-scipy() {
  depends=("${MINGW_PACKAGE_PREFIX}-openblas"
           "${MINGW_PACKAGE_PREFIX}-python2-numpy")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose: testsuite")

  cd ${_realname}-py2-${pkgver}
  LDFLAGS="${LDFLAGS} -Wall -shared"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  install -Dm644 LICENSE.txt \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_python3-scipy() {
  depends=("${MINGW_PACKAGE_PREFIX}-openblas"
           "${MINGW_PACKAGE_PREFIX}-python3-numpy")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-nose: testsuite")

  cd ${_realname}-py3-${pkgver}
  LDFLAGS="${LDFLAGS} -Wall -shared"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  install -Dm644 LICENSE.txt \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-scipy() {
  package_python2-scipy
}

package_mingw-w64-i686-python3-scipy() {
  package_python3-scipy
}

package_mingw-w64-x86_64-python2-scipy() {
  package_python2-scipy
}

package_mingw-w64-x86_64-python3-scipy() {
  package_python3-scipy
}
